﻿using System;
using System.Web.UI.HtmlControls;

using OctoOS;
using OctoOS.UI;

public partial class __admin_default : OOSPage
{
	protected void Page_Init(object sender, EventArgs e)
	{
		OOSRequest.Current.EnsureModerator();
	}
	
    protected void Page_Load(object sender, EventArgs e)
    {
		PageTitle = Resources.Main.settings_Title;

		Commands.AddCommand(Resources.Main.settings_AddCategory, Urls.Admin.GetAddCategory());

		if(!IsPostBack)
		{
			AddBreadcrumb(string.Empty, "/");
			var resourceInfo = AppUtil.GetResourceInfo(OOSRequest.Current);

			//settings
			var settings = resourceInfo.Settings;
			commentsEnabled.SelectedValue = settings.CommentsEnabled.ToString();
			commentsModeration.SelectedValue = settings.CommentsModeration.ToString();
			disableAfter.Value = settings.DisableAfter;
			feedMode.SelectedValue = settings.FeedMode.ToString();
			articleSettings.Items[0].Selected = settings.EnableSubTitle;
			articleSettings.Items[1].Selected = settings.SyndicatePosts;
			articleToolbar.Items[0].Selected = settings.Bookmarks;
			articleToolbar.Items[1].Selected = settings.SendToFriend;
			articleToolbar.Items[2].Selected = settings.Print;
			//articleToolbar.Items[3].Selected = resource.TrackbarUrl; закомментировано для trackbackUrl
			articleToolbar.Items[3].Selected = settings.CommentRss;

			//categories
			var categories = resourceInfo.Categories;
			if(!CollectionUtility.IsNullOrEmpty(categories))
			{
				foreach(var category in categories)
				{
					categoriesTree.Nodes.AddContentNode(
						GetCategoryLink(category));
				}
			}
			else
			{
				emptyBox.Visible = true;
			}
		}
    }

	private HtmlControl[] GetCategoryLink(Category category)
	{
		HtmlAnchor[] links = new HtmlAnchor[2];

		links[0] = new HtmlAnchor()
		{
			ID = category.CategoryID.ToString(),
			InnerText = category.Title,
			HRef = Urls.Admin.GetEditCategory(category.CategoryID)
		};
		links[0].Style.Add("margin-right", "20px");

		links[1] = (new HtmlAnchor()
		{
			InnerText = Resources.Main.shared_Delete,
			HRef = Urls.Admin.GetDeleteCategory(category.CategoryID)
		});

		return links;
	}

	protected void SubmitClick(object sender, EventArgs e)
	{
		if(IsValid)
		{
			ApiService.SaveSettings(OOSRequest.Current,
				(CommentsEnabled)Enum.Parse(typeof(CommentsEnabled), commentsEnabled.SelectedValue, true),
				(CommentsModeration)Enum.Parse(typeof(CommentsModeration), commentsModeration.SelectedValue, true),
				(FeedMode)Enum.Parse(typeof(FeedMode), feedMode.SelectedValue, true),
				disableAfter.ValueAsInt32,
				articleSettings.Items[0].Selected,
				articleSettings.Items[1].Selected,
				articleToolbar.Items[0].Selected,
				articleToolbar.Items[1].Selected,
				articleToolbar.Items[2].Selected,
				//false,
				articleToolbar.Items[3].Selected);

			AppUtil.UpdateSettings(OOSRequest.Current);

			Response.Redirect("/");
		}
	}
}